package com.jiukuaidao.zhulin.module.order.presenter;

import com.jiukuaidao.library.net.MyRetroCallBack;
import com.jiukuaidao.zhulin.api.ApiGetOrderList;
import com.jiukuaidao.zhulin.entity.ApiOrderList;
import com.jiukuaidao.zhulin.module.BasePresenter;
import com.jiukuaidao.zhulin.module.order.contract.OrderListContract;

import java.util.ArrayList;
import java.util.List;

/**
 * Author : PaiTaSuo
 * Date : 2016/6/3
 * Description :
 */
public class OrderListPresenter extends BasePresenter<OrderListContract.View, OrderListContract.Model> implements OrderListContract.Presenter {

    public OrderListPresenter(OrderListContract.Model repository, OrderListContract.View view) {
        super(repository, view);
    }

    @Override
    public void start() {
        loadData();
    }

    @Override
    public void loadData() {
        mRepository.loadData(new ApiGetOrderList<>(mView.getIndex(), mView.getStatus(), new MyRetroCallBack<ApiOrderList>() {
            @Override
            public void onSuccess(ApiOrderList result, String errorCode, String msg) {
                if (result != null) {
                    List<ApiOrderList.OrderList> orderList = result.list;
                    if (mRepository.geOrderList() == null) {
                        mRepository.setOrderList(new ArrayList<ApiOrderList.OrderList>());
                    }

                    if (mView.refreshOrLoad() == OrderListContract.REFRESH) {
                        mRepository.geOrderList().clear();
                    }

                    if (orderList != null) {
                        mRepository.geOrderList().addAll(orderList);
                    }

                    if (mRepository.geOrderList() != null && !mRepository.geOrderList().isEmpty()) {
                        mView.orderList(mRepository.geOrderList());
                    } else {

//                        mView.toast("暂无订单");
                    }
                }
            }

            @Override
            public void onError(Throwable t, int resCode, String msg) {
//                mView.toast(resCode + "");
            }

            @Override
            public void onFail(ApiOrderList result, String errorCode, String msg) {
//                mView.toast(msg);
            }
        }));
    }
}
